package com.gyt.manage.cms.service.impl;

import com.alibaba.fastjson.JSON;
import com.gyt.framework.domain.cms.CmsPage;
import com.gyt.manage.cms.dao.CmsRepository;
import com.gyt.manage.cms.service.CmsService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author created by v_yantgao
 * @Date 2021/1/11 11:00
 */
@Service
public class CmsServiceImpl implements CmsService {

    private final static Logger logger = LoggerFactory.getLogger(CmsServiceImpl.class);

    @Autowired
    private CmsRepository cmsRepository;
    @Autowired
    private RestTemplate restTemplate;
    @Override
    public CmsPage findByPageName(String pageName){
        Optional<CmsPage> optional = cmsRepository.findById("d");
        if (optional.isPresent()) {
            CmsPage cmsPage = optional.get();

        }
        List<CmsPage> cmsPages = cmsRepository.findAll();
        logger.info("cmsPages:{}",JSON.toJSONString(cmsPages));
        CmsPage cmsPage = cmsRepository.findByPageName(pageName);
        logger.info("cmsPage:{}",JSON.toJSONString(cmsPage));
        return cmsPage;
    }
    //http://localhost:9093/resource/cms/findByPageName.do?pageName=index.html

    @Test
    public void testRestTemplate(){
        ResponseEntity<Map> entity = restTemplate.getForEntity("http://localhost:9093/resource/cms/findByPageName.do?pageName=index.html", Map.class);
        if (entity.hasBody()){
            Map map = entity.getBody();
            String siteId = map.get("siteId") != null ? map.get("siteId").toString() : null;//5a751fab6abb5044e0d19ea1
            System.out.println("siteId:" + siteId);
        }
    }
}
